from flask import Blueprint, jsonify, request
from src.models import Teacher, db

teacher_bp = Blueprint('teacher', __name__)

@teacher_bp.route('/', methods=['GET'])
def get_teachers():
    """获取所有教师列表"""
    teachers = Teacher.query.all()
    return jsonify({
        'status': 'success',
        'data': [{
            '教师编号': teacher.教师编号,
            '教师姓名': teacher.教师姓名
        } for teacher in teachers]
    })

@teacher_bp.route('/<string:teacher_id>', methods=['GET'])
def get_teacher(teacher_id):
    """获取指定教师信息"""
    teacher = Teacher.query.get_or_404(teacher_id)
    return jsonify({
        'status': 'success',
        'data': {
            '教师编号': teacher.教师编号,
            '教师姓名': teacher.教师姓名
        }
    })

@teacher_bp.route('/', methods=['POST'])
def create_teacher():
    """创建新教师"""
    data = request.get_json()
    
    # 验证数据
    if not data or not data.get('教师编号') or not data.get('教师姓名'):
        return jsonify({
            'status': 'error',
            'message': '缺少必要的教师信息'
        }), 400
        
    # 检查教师编号是否已存在
    if Teacher.query.get(data['教师编号']):
        return jsonify({
            'status': 'error',
            'message': '教师编号已存在'
        }), 400
        
    # 创建新教师
    teacher = Teacher(
        教师编号=data['教师编号'],
        教师姓名=data['教师姓名']
    )
    
    try:
        db.session.add(teacher)
        db.session.commit()
        return jsonify({
            'status': 'success',
            'message': '教师创建成功',
            'data': {
                '教师编号': teacher.教师编号,
                '教师姓名': teacher.教师姓名
            }
        }), 201
    except Exception as e:
        db.session.rollback()
        return jsonify({
            'status': 'error',
            'message': f'教师创建失败：{str(e)}'
        }), 500

@teacher_bp.route('/<string:teacher_id>', methods=['PUT'])
def update_teacher(teacher_id):
    """更新教师信息"""
    teacher = Teacher.query.get_or_404(teacher_id)
    data = request.get_json()
    
    if not data:
        return jsonify({
            'status': 'error',
            'message': '未提供更新数据'
        }), 400
        
    # 更新教师姓名
    if '教师姓名' in data:
        teacher.教师姓名 = data['教师姓名']
        
    try:
        db.session.commit()
        return jsonify({
            'status': 'success',
            'message': '教师信息更新成功',
            'data': {
                '教师编号': teacher.教师编号,
                '教师姓名': teacher.教师姓名
            }
        })
    except Exception as e:
        db.session.rollback()
        return jsonify({
            'status': 'error',
            'message': f'教师信息更新失败：{str(e)}'
        }), 500

@teacher_bp.route('/<string:teacher_id>', methods=['DELETE'])
def delete_teacher(teacher_id):
    """删除教师"""
    teacher = Teacher.query.get_or_404(teacher_id)
    
    try:
        db.session.delete(teacher)
        db.session.commit()
        return jsonify({
            'status': 'success',
            'message': '教师删除成功'
        })
    except Exception as e:
        db.session.rollback()
        return jsonify({
            'status': 'error',
            'message': f'教师删除失败：{str(e)}'
        }), 500

@teacher_bp.route('/<string:teacher_id>/courses', methods=['GET'])
def get_teacher_courses(teacher_id):
    """获取教师的授课列表"""
    teacher = Teacher.query.get_or_404(teacher_id)
    courses = [{
        '授课编号': teaching.授课编号,
        '课程编号': teaching.课程编号,
        '课程名称': teaching.course.课程名称,
        '教室': teaching.教室
    } for teaching in teacher.授课记录]
    
    return jsonify({
        'status': 'success',
        'data': courses
    }) 